<template>
  <div class="component-upload-image">
    <el-upload
      :action="uploadImgUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      :on-exceed="handleExceed"
      name="file"
      :show-file-list="false"
      :headers="headers"
      class="image-upload"
			:class="['size-' + size]"
      ref="imageUploader"
    >
      <img v-if="imageUrl" :src="imageUrl" class="image">
      <i class="el-icon-plus"></i>
					 <el-button type="danger" v-if="clearable && imageUrl"  size="mini" class="btn-remove " icon="el-icon-close" circle @click.stop="clear()"></el-button>
    </el-upload>

    <!-- 上传提示 -->
    <div class="el-upload__tip" slot="tip" v-if="showTip">
      请上传
      <template v-if="fileSize"> 大小不超过 <b>{{ fileSize }}MB</b> </template>
      <template v-if="fileType"> 格式为：<b>{{ fileType.join("/") }}</b> </template>
					 <template v-if="format"> 尺寸为：<b>{{ format}}</b></template>
      的文件
    </div>

  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  props: {
    value: [String],

    // 大小限制(MB)
    fileSize: {
       type: Number,
      default: 5,
    },
			 // 尺寸规格 例如 750 * 300
				format: {
					type: String
				},
    // 文件类型, 例如['png', 'jpg', 'jpeg']
    fileType: {
      type: Array,
      default: () => ["png", "jpg", "jpeg"],
    },
    // 是否显示提示
    isShowTip: {
      type: Boolean,
      default: true
    },
    clearable: {
      type: Boolean,
      default: false
    },
    size:{
      type: String,
      default : 'normal'
    },
   // 是否上传 到临时目录
    temporary:{
       type:Boolean,
       default: false
    }
  },
  data() {
    return {
      hideUpload: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      uploadImgUrl: process.env.VUE_APP_BASE_API + (this.temporary ? "/common/temp" : "/common/upload"), // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      }
    };
  },
  computed: {
    /**
     * 图片地址
     */
    imageUrl(){
      if(!this.value){
        return null;
      }
      if(this.value.indexOf('http://') == 0 || this.value.indexOf('https://') == 0 || this.value.indexOf('//') == 0){
        return this.value;
      }
      return this.baseUrl + this.value;
    },
    // 是否显示提示
    showTip() {
      return this.isShowTip && (this.fileType || this.fileSize);
    },
    /**
     * 样式
     * @returns {{}|{width: string, height: string}}
     */
    style(){
      if(this.format){
        let arr = this.format.split("*");
        if(arr.length == 2){
          let width = arr[0] * 1;
          let height = arr[1] * 1;



          return {
            width: Math.floor(width / height * HEIGHT)  + 'px',
            height: HEIGHT + 'px'
          }
        }
      }
      return {

      }
    }
  },
  mounted() {
    let imageUploader = this.$refs.imageUploader.$el;
    if(this.style.width){
      imageUploader.style.width = this.style.width;
    }
    if(this.style.height){
      imageUploader.style.height = this.style.height;
    }

  },
  methods: {

    // 上传成功回调
    handleUploadSuccess(resp) {
      this.$emit("input", resp.fileName);
      this.loading.close();
    },
			 clear(){
					this.$emit("input", '');
				},
    // 上传前loading加载
    handleBeforeUpload(file) {
      let isImg = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isImg = this.fileType.some(type => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isImg = file.type.indexOf("image") > -1;
      }

      if (!isImg) {
        this.$message.error(
          `文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
        );
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$message.error(`上传图片大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    // 文件个数超出
    handleExceed() {
      this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传失败
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    }
  }
};
</script>
<style scoped lang="scss">
// .el-upload--picture-card 控制加号部分
::v-deep.hide .el-upload--picture-card {
    display: none;
}
// 去掉动画效果
::v-deep .el-list-enter-active,
::v-deep .el-list-leave-active {
    transition: all 0s;
}

::v-deep .el-list-enter, .el-list-leave-active {
    opacity: 0;
    transform: translateY(0);
}
.image{
  width:100%;
  height:100%;
}
.image-upload{
  position:relative;
  width:148px;
  height:148px;

	 &.size-small{
			 width: 120px;
				height: 120px;
		}

	&.size-mini{
		width: 100px;
		height: 100px;
	}

	.btn-remove{
		position: absolute;
		top: -10px;
		right: -10px;
		display: none;
	}

	&:hover{
		.btn-remove{
			display: block;
		}
	}

}

::v-deep .image-upload .el-upload--picture-card{
	width: 100%!important;
	height: 100%!important;
	.btn-remove{
		i{
			font-size: 14px;
			color: #FFFFFF;
		}
	}
}

.el-icon-plus{
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%, -50%)
}

b{
	color: #f56c6c
}
</style>

